/**
 * @Title: UserModal.js
 * @Description: UserModal
 * @author: Fanchao
 * @date: 2020/8/21 12:36 上午
 * @version V1.0
 */

import React, { memo } from 'react';
import { connect } from 'umi';
import { Cascader, Form, Input, Modal } from 'antd';

const layout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 16 },
};

const UserModal = memo(({ administrators: { itemData, treeData }, dispatch, title, visible }) => {
    const [form] = Form.useForm();
    form.setFieldsValue(itemData);
    return (
        <Modal
            title={title}
            visible={visible}
            width="960px"
            onOk={() => {
                form.validateFields().then(values => {
                    values.leastDepartmentId = values.leastDepartmentId.pop();
                    values.userRoleType = 'Admin';
                    dispatch({
                        type: 'administrators/addAdminUser',
                        payload: values,
                        callback: (result) => {
                            if (result) {
                                form.resetFields();
                                dispatch({
                                    type: 'administrators/setItemData',
                                    payload: {},
                                });
                                dispatch({
                                    type: 'administrators/setModalVisible',
                                    payload: false,
                                });
                                dispatch({
                                    type: 'administrators/getDataSource',
                                    payload: {
                                        keyword: '',
                                    },
                                });
                            }
                        },
                    });

                }).catch(info => {
                });
            }}
            onCancel={() => {
                form.resetFields();
                dispatch({
                    type: 'administrators/setItemData',
                    payload: {},
                });
                dispatch({
                    type: 'administrators/setModalVisible',
                    payload: false,
                });
            }}
        >
            <Form
                {...layout}
                layout="horizontal"
                form={form}
                name="basic"
                // 表单默认值，只有初始化以及重置时生效 莫用
                initialValues={{
                    // name: itemData.name
                }}
            >
                <Form.Item
                    label="管理员姓名"
                    name="realName"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Input placeholder="请输入管理员姓名"/>
                </Form.Item>
                <Form.Item
                    label="登录账号"
                    name="loginId"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Input placeholder="请输入登录账号"/>
                </Form.Item>
                <Form.Item
                    label="登录密码"
                    name="password"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Input placeholder="请输入登录密码"/>
                </Form.Item>
                <Form.Item
                    label="证件号"
                    name="credentialsNo"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Input placeholder="请输入证件号"/>
                </Form.Item>
                <Form.Item
                    label="所在单位"
                    name="leastDepartmentId"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Cascader
                        changeOnSelect
                        options={treeData}
                    />
                </Form.Item>
                <Form.Item
                    label="备注"
                    name="comment"
                    rules={[{ required: false, message: '' }]}
                >
                    <Input.TextArea placeholder="请输入备注"/>
                </Form.Item>
            </Form>
        </Modal>
    );
});

export default connect(({ administrators, dispatch }) => ({
    administrators,
    dispatch,
}))(UserModal);
